<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>供货商管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../css/customer_list.css" media="all">
</head>
<style>
    #roleAuthTable + .layui-table-view .layui-table tbody tr:hover {
        background-color: transparent;
    }
    .layui-input-block {
        margin-left: 95px;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">关键字</label>
                        <div class="layui-input-inline mr0">
                            <input id="keyWords" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select id="state">
                                <option value="" checked>全部状态</option>
                                <option value="0">正常</option>
                                <option value="1">关闭</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">到期时间</label>
                        <div class="layui-input-inline">
                            <input type="text" placeholder="时间段" class="layui-input" id='due_date'>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnReset" class="layui-btn layui-btn-warm"><i class="layui-icon">&#xe669;</i>重置
                        </button>
                        <button id="roleBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="infoTable" lay-filter="infoTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="infoTableBar">
    <a class="layui-btn-a" lay-event="edit">修改</a>
    <a class="layui-btn-a btn-red" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="companyForm">
    <form lay-filter="companyForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">公司名称</label>
            <div class="layui-input-block">
                <input name="suppliers_company_name" placeholder="请输入公司名称" type="text" class="layui-input"
                       maxlength="155" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-inline more_in">
                <input type="text" name="contact_person_name" placeholder="请输入联系人姓名" autocomplete="off" class="layui-input" lay-verify="required" required>
            </div>
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline">
                <input type="text" name="contact_person_phone" placeholder="请输入联系人电话" autocomplete="off" class="layui-input" lay-verify="required" required>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">文件模版</label>
            <div class="layui-input-block">
                <select name="file_template" id="file_template" lay-search lay-verify="required" required>
                    <option value="">请选择</option>
                    <option value="ht">鸿天</option>
                    <option value="cy">车友</option>
                    <option value="personal">个体户</option>
                </select>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">所在地区</label>
            <div class="layui-input-inline selectArea more_in">
                <select name="province" id="province" lay-filter="province" lay-search lay-verify="required" required>
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline selectArea">
                <select name="city" id="city" lay-filter="city" lay-search lay-verify="required" required>
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline selectArea">
                <select name="district" id="district" lay-search>
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详细地址</label>
            <div class="layui-input-block">
                <input type="text" name="address_detail" placeholder="请输入详细地址" autocomplete="off" class="layui-input" lay-verify="required" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">服务品牌</label>
            <div class="layui-input-block">
                <input type="text" name="service_brand" placeholder="多个品牌用逗号分开" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label must">税率</label>
            <div class="layui-input-inline more_in">
                <input name="tax_rate" placeholder="请输入税率" type="number" class="layui-input"
                       max="100" lay-verify="required" required/>
            </div>
            <div class="layui-form-mid layui-word-aux">%</div>
            <label class="layui-form-label must">到期时间</label>
            <div class="layui-input-inline">
                <input type="text" name="due_time" id="due_time" class="layui-input" placeholder="yyyy-MM-dd" lay-verify="required" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label must">登录账号</label>
            <div class="layui-input-inline more_in" style="margin-right: 32px">
                <input type="text" name="username" placeholder="请输入平台登录账号" autocomplete="off" class="layui-input" lay-verify="required" required>
            </div>
            <label class="layui-form-label must">登录密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" placeholder="请输入平台登录密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux" style="display: none" id="pwd"><span style="color: red;">(不修改无需填写)</span></div>
        </div>
        <div class="layui-form-item text-center">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="companyFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="infoTbaleState">
    <input type="checkbox" lay-filter="ckState" lay-skin="switch" lay-text="正常|关闭"
           value="{{d.id}}" {{d.state==1?'':'checked'}}/>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../common/layui/layui2.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>
<script type="text/javascript" src="../../js/area.js"></script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table','admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;

        var cityList = {};      //市
        var districtList = {};	//区县

        laydate.render({
            elem: '#due_date'
            ,range: true
            ,trigger: 'click'
        });

        form.render();

        //渲染表格
        table.render({
            elem: '#infoTable',
            url: access_baseurl+'Suppliers/getList',
            where: {
            },
            page: true,
            pageSize:20,
            cellMinWidth: 60,
            cols: [[
                {field: 'suppliers_company_name', title: '公司名称'},
                {field: 'service_brand', title: '服务品牌'},
                {field: 'address', title: '所在地址'},
                {field: 'contact_person_name', title: '联系人',width:130},
                {field: 'contact_person_phone', title: '联系电话',width:130},
                {field: 'tax_rate', title: '税率',width:60,
                    templet:function (d) {
                        return d.tax_rate?d.tax_rate+'%':'';
                    }},
                {field: 'due_time', title: '到期时间',width:130},
                {field: 'state', toolbar: '#infoTbaleState',title: '状态',width:100},
                {align: 'left', toolbar: '#infoTableBar', title: '操作', width: 150}
            ]]
        });
        // 添加按钮点击事件
        $('#roleBtnAdd').click(function () {
            showEditModel();
        });
        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var where = getWhere();
            table.reload('infoTable', {where: where});
        });
        $('#btnReset').click(function () {
            $('#keyWords').val('');
            $('#state').val('');
            $('#due_date').val('');
            form.render();
            var where = getWhere();
            table.reload('infoTable', {where: where});
        });
        // 工具条点击事件
        table.on('tool(infoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(data.id);
            }
        });

        // 修改状态
        form.on('switch(ckState)', function (obj) {
            layer.load(2);
            send_req('Suppliers/changeState',
                {
                    id: obj.elem.value,
                    state: obj.elem.checked ? 0 : 1
                },
                function (data) {
                    layer.closeAll('loading');
                    layer.msg('修改成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
            });
            return false;
        });

        // 删除
        function doDelete(id) {
            layer.confirm('确定要删除吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Suppliers/del', {id:id}, function (res) {
                    layer.closeAll('loading');
                    layer.msg('删除成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
                });
                return false;
            });
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '800px',
                offset: '65px',
                title: data ? '修改供货商信息' : '添加供货商信息',
                content: $('#companyForm').html(),
                success: function (layero, index) {
                    // console.log(layero)
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    generateFirstLevelarea();
                    laydate.render({
                        elem: '#due_time'
                        ,trigger: 'click'
                        ,type: 'date'
                    });
                    form.render();
                    if (data) {
                        $('#pwd').show();
                        send_req('Suppliers/getInfo', {id: data.id, showCity: 1}, function (res) {
                            var dataInfo = res;
                            if (dataInfo) {
                                if(dataInfo.province){
                                    getSencondLevelarea(dataInfo.province);
                                    form.val('companyForm',dataInfo)
                                    form.render();
                                    if(dataInfo.city){
                                        getThreeLevelarea(dataInfo.city);
                                    }
                                }
                                form.val('companyForm', dataInfo);
                                form.render();
                            }
                        },true);
                    }
                }
            })
        }

        form.on('submit(companyFormSubmit)', function (d) {
            if(d.field.district){
                d.field.address_area = d.field.province+' '+d.field.city+' '+d.field.district;
            }else if(d.field.city){
                d.field.address_area = d.field.province+' '+d.field.city;
            }else {
                d.field.address_area = d.field.province?d.field.province:'';
            }
            layer.load(2);
            send_req('Suppliers/modifyCompany', d.field, function (res) {
                layer.closeAll('page');
                layer.closeAll('loading');
                layer.msg('操作成功', {icon: 1});
                table.reload('infoTable');
            });
            return false;
        });

        function getWhere() {
            var due_date = $('#due_date').val();
            var words = $('#keyWords').val();
            var state = $('#state').val();

            var where = {
                due_date: due_date,
                words: words,
                state:state
            };
            return where;
        }
        // 导出
        $('#export').click(function () {
            var where = getWhere();
            table.reload('infoTable', {where: where});
            location.href = serverUrl + 'Suppliers/dataExport.html?state='+where.state+'&words='+where.words
                +'&due_date='+where.due_date;
        });

        /***************地址区域初始化**********************/
        function generateFirstLevelarea() {
            var area_first_str = '<option value="">请选择</option>';
            for (var i = 0; i < provinceList.length; i++) {
                area_first_str += '<option value="' + provinceList[i].value + '">' + provinceList[i].value + '</option>';
            }
            $('#province').html(area_first_str);
        }

        form.on('select(province)',function (data) {
            var selectedFirstLevel = data.value;
            // 清空并隐藏第二、三级菜单
            $('#city').empty().attr('disabled', true);
            $('#district').empty().attr('disabled', true);
            // 生成第二级菜单
            getSencondLevelarea(selectedFirstLevel)
            form.render();
        });

        function getSencondLevelarea(selectedFirstLevel){
            var secondLevelOptions = '';
            if (selectedFirstLevel) {
                var firstLevelData = provinceList.find(item => item.value === selectedFirstLevel);
                if (firstLevelData && firstLevelData.children && firstLevelData.children.length > 0) {
                    secondLevelOptions = '<option value="">请选择</option>';
                    for (var j = 0; j < firstLevelData.children.length; j++) {
                        secondLevelOptions += '<option value="' + firstLevelData.children[j].value + '">' + firstLevelData.children[j].value + '</option>';
                    }
                    $('#city').html(secondLevelOptions).attr('disabled', false);
                    form.render();
                    // 预备生成第三级菜单的逻辑，这里假设第二级选择后会填充第三级
                }
            }
        }
        form.on('select(city)',function (data) {
            var selectedSecondLevel = data.value;
            getThreeLevelarea(selectedSecondLevel)
            form.render();
        });
        function getThreeLevelarea(selectedSecondLevel){
            var selectedFirst = $('#province').val();
            var firstLevelData = provinceList.find(item => item.value === selectedFirst);
            var secondLevelData = firstLevelData.children.find(item => item.value === selectedSecondLevel);
            if (secondLevelData && secondLevelData.children && secondLevelData.children.length > 0) {
                var thirdLevelOptions = '<option value="">请选择</option>';
                for (var k = 0; k < secondLevelData.children.length; k++) {
                    thirdLevelOptions += '<option value="' + secondLevelData.children[k].value + '">' + secondLevelData.children[k].value + '</option>';
                }
                $('#district').html(thirdLevelOptions).attr('disabled', false).parent().show();
            } else {
                $('#district').empty().attr('disabled', true);
            }
            form.render();
        }
        /***************地址区域初始化**********************/

    });
</script>

</body>
</html>